using UnityEngine;

namespace Gj
{
    public class AndroidScreen
    {
        private static AndroidJavaObject currentActivity
        {
            get
            {
                return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            }
        }
 
        /// <summary>
        ///  隐藏上方状态栏
        /// </summary>
        public static void HideStatusBar()
        {
#if UNITY_ANDROID
            currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                currentActivity.Call<AndroidJavaObject>("getWindow").Call("clearFlags", 2048);
 
            }));
#endif
            Debug.Log("hide status bar");
        }
 
        /// <summary>
        ///  显示上方状态栏
        /// </summary>
        public static void ShowStatusBar()
        {
#if UNITY_ANDROID
            currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                currentActivity.Call<AndroidJavaObject>("getWindow").Call("addFlags", 2048);// WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
            }));
#endif
            Debug.Log("show status bar");
        } 
        
        public static void SafeArea(bool isSetNotchScreen)
        {
#if UNITY_ANDROID
            currentActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                AndroidJavaObject currentWindow = currentActivity.Call<AndroidJavaObject>("getWindow");//返回Window
 
                AndroidJavaObject lp = currentWindow.Call<AndroidJavaObject>("getAttributes");//返回WindowManager.LayoutParams
                if (isSetNotchScreen)
                {
                    //int WindowManager_LayoutParams_FLAG_LAYOUT_IN_OVERSCAN = 33554432;//允许窗口内容扩展到屏幕的过扫描区域（如果有）。窗口仍应正确定位其内容以考虑过扫描区域。
                    int WindowManager_LayoutParams_FLAG_TRANSLUCENT_STATUS = 67108864;//有透明的状态栏和导航栏，且屏幕延伸至状态栏内部
                    lp.Set("flags", WindowManager_LayoutParams_FLAG_TRANSLUCENT_STATUS);
                }
                else
                {
                    int WindowManager_LayoutParams_FLAG_LAYOUT_IN_SCREEN = 256;
                    lp.Set("flags", WindowManager_LayoutParams_FLAG_LAYOUT_IN_SCREEN);
                }
                currentWindow.Call("setAttributes", lp);
            }));
#endif
        }
    }
}